package com.neucloud.da.edgeside.business;

import com.alibaba.fastjson.JSONObject;
import com.neucloud.da.edgeside.model.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

@Slf4j
@Service
public class ReqBaseBusiness {

    @Autowired
    HttpServletRequest request;

    @Resource(name = "commonTemplate")
    RestTemplate onlineTemplate;

    /**
     * @MethodName reqeustForward
     * @Description 请求转发服务
     * @Param
     * @param servicePath
     * @param reqUrl
     * @param reqParam
     * @param reqBody
     * @param reqMethod
     * @return org.springframework.http.ResponseEntity
     * @Author luoyueqiang
     * @Date 2022/9/25 16:25
     **/
    public ResponseEntity reqeustForward(String servicePath, String reqUrl, String reqParam, String reqBody, String reqMethod){
        HttpHeaders headers = new HttpHeaders();
        if(!StringUtils.isEmpty(reqUrl)){
            servicePath = servicePath +  "/" + reqUrl;
        }
        if (!StringUtils.isEmpty(reqParam)) {
            servicePath = servicePath + "?" + reqParam;
        }
        JSONObject body = null;
        if (!StringUtils.isEmpty(reqBody)) {
            body = JSONObject.parseObject(reqBody);
        }
        HttpEntity<JSONObject> httpEntity = null;
        ResponseEntity resp = null;
        if(body == null){
            httpEntity = new HttpEntity<>(headers);
            resp = onlineTemplate.exchange(servicePath, HttpMethod.valueOf(reqMethod), httpEntity, String.class);
        }else{
            httpEntity = new HttpEntity<>(body, headers);
            resp = onlineTemplate.exchange(servicePath, HttpMethod.valueOf(reqMethod), httpEntity, String.class);
        }
        return resp;
    }

    /**
     * @MethodName getRequestBody
     * @Description 获取请求的body
     * @Param
     * @return java.lang.String
     * @Author luoyueqiang
     * @Date 2022/9/25 16:25
     **/
    protected String getRequestBody(){
        StringBuffer data = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = request.getReader();
            if(request==null){
                log.error("can not get current request");
            }
            String line = null;
            while (null != (line = reader.readLine())){
                data.append(line);
            }
            reader.close();
        } catch (IOException e) {
            log.error("IOException: ", e);
            throw new BusinessException("读取BODY异常");
        } finally {
            if(null != reader){
                try
                {
                    reader.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return data.toString();
    }

    /**
     * @MethodName getReqeustMethod
     * @Description 获取请求Method
     * @Param
     * @return java.lang.String
     * @Author luoyueqiang
     * @Date 2022/9/25 16:16
     **/
    protected String getReqeustMethod(){
        return request.getMethod();
    }

    /**
     * @MethodName getReqeustParams
     * @Description 获取请求参数
     * @Param
     * @return java.lang.String
     * @Author luoyueqiang
     * @Date 2022/9/25 16:16
     **/
    protected String getReqeustParams(){
        return request.getQueryString();
    }

}
